package com.cby.olive.sample

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.EditText
import androidx.fragment.app.Fragment
import com.cby.android.olive.ext.*
import com.cby.android.olive.utils.AppUtils
import com.cby.olive.sample.databinding.FragmentMainBinding
import com.cby.olive.sample.model.Coordinate
import com.cby.olive.sample.model.Location
import com.cby.olive.sample.model.Person
import com.cby.olive.sample.model.persons

/**
 * @description: MainFragment
 *
 * @author ChenBaoyang
 * @date 2021/11/4 14:42
 */
class MainFragment : Fragment(R.layout.fragment_main) {

    private val binding by viewBinding(FragmentMainBinding::bind)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.ivSetting.setOnClickListener {
            activity?.start(SettingsActivity::class.java)
        }

        //dsl写法
        binding.editTest.addAfterTextChange {
            afterTextChanged {
                showToast(binding.editTest.text.toString())
            }
        }

        val appName = AppUtils.getAppName(requireContext(), requireContext().packageName)
        showToast("appName = $appName")
        //高阶函数写法
//        editText.afterTextChange {
//
//        }

        binding.editTest.setShow(){
            showToast("setShow = $appName")
        }

        binding.btnWindow.setOnClickListener {
            activity?.start(WindowCheckActivity::class.java)
        }

        // 测试列表的日志打印
        persons.print { "${it.name}-${it.age}" }.let { Log.v("test",it) }

        // 测试复杂数据类型的日志打印
        Person("Peter", 16, Location("china", "shanghai", Coordinate(10, 20)))
            .ofMap()
            ?.print()
            .let { Log.v("test", "$it") }
    }
}